<template>
	<view>
		<view class="header" v-bind:class="{'status':isH5Plus}">
			<view class="userinfo" v-if="token!=''">
				<view class="face" @click="uploadImag()">
					<image :src="userinfo.photo.photoUrl"></image>
				</view>
				<view class="info">
					<view class="username" style="margin-top: 30rpx;">{{userinfo.username}}</view>
				</view>
			</view>
			<!-- 未登录 -->
			<view class="userinfo" v-if="token==''" @tap=login()>
				<view class="face">
					<image src="../../static/face.jpg"></image>
				</view>
				<view class="info">
					<view class="username" style="margin-top: 30rpx;">点击登录</view>
				</view>
			</view>
		</view>
		<view v-if="token!=''">
			<!-- 单元格 -->
			<view v-if="authority==1">
				<u-cell-group>
					<!-- <u-cell icon="edit-pen" title="编辑资料"></u-cell> -->
					<u-cell icon="tags" @tap="editorCandidate()" title="简历信息"></u-cell>
				</u-cell-group>
			</view>

			<view v-if="authority==2">
				<u-cell-group>
					<u-cell icon="list" title="企业信息列表" url="/pageB/enterpriseList/enterpriseList"></u-cell>
					<u-cell icon="list" title="待审核的企业" url="/pageB/examineEnterprise/examineEnterprise"></u-cell>
					<u-cell icon="list" title="审核失败的企业" url="/pageB/examineFailed/examineFailed"></u-cell>
					<u-cell icon="list" title="封禁的企业" url="/pageB/banEnterprise/banEnterprise"></u-cell>
					<u-cell icon="edit-pen" title="职位信息维护" url="/pageB/recruitmentInfo/recruitmentInfo"></u-cell>
				</u-cell-group>
			</view>

			<view>
				<u-popup :show="show" mode="center" @close="close" @open="open">
					<view>
						<u-button type="primary" :plain="true" @tap="closeTab(2)" text="我是企业,我要招聘人才"></u-button>
						<u-button type="primary" :plain="true" @tap="closeTab(1)" text="我是求职者,我要找工作"></u-button>
					</view>
				</u-popup>
				<u-button @click="show = true">角色切换</u-button>
			</view>
			<view style="margin-top: 20upx;">
				<u-button type="primary" v-if="token!=''" @tap="loginOut()" :plain="true" text="退出当前账号"></u-button>
			</view>

		</view>
	</view>
</template>

<script>
	import IMService from "../../lib/imservice.js"
	export default {
		data() {
			return {
				token: '',
				userinfo: {
				},
				show: false,
				authority: 1
			}
		},
		methods: {
			editorCandidate() {
				uni.navigateTo({
					url: `/pageB/editorCandidate/editorCandidate?id=${this.userinfo.tbCandidates.id}`
				})
			},
			closeTab(num) {
				if(num==2){
					uni.setTabBarItem({
						index:1,
						text:"找人",
						iconPath:'/static/tabbar-icons/feeds.png',
						selectedIconPath:'/static/tabbar-icons/feeds_s.png'
					});
				}else{
					uni.setTabBarItem({
						index:1,
						text:"找活儿",
						iconPath:'/static/tabbar-icons/feeds.png',
						selectedIconPath:'/static/tabbar-icons/feeds_s.png'
					});
				}
				getApp().globalData.authority = num;
				this.authority = num
				this.show = false
			},
			login() {
				uni.navigateTo({
					url: "/pages/login/login"
				})
			},
			checkLogin() {
				let token = uni.getStorageSync('token');
				this.token = token
				if (token == '') {

				} else {
					this.userinfo = uni.getStorageSync('user')
				}
			},
			loginOut() {
				if (this.goEasy.getConnectionStatus() === 'disconnected') {
					getApp().globalData.imService = new IMService(this.goEasy, this.GoEasy);
					getApp().globalData.imService.connect(this.userinfo);
				}
				this.goEasy.disconnect({
				    onSuccess: function(){
				        console.log('断连成功')
				        getApp().globalData.imService = null;
				    },
				    onFailed: function(error){
				        uni.showToast({
				            title:'断连失败'
				        })
				    }
				});
				this.userinfo = {}
				this.token = ''
				uni.setStorageSync("token", '')
				uni.setStorageSync("user", '')
				getApp().globalData.user = {}
				this.checkLogin()
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false
				// console.log('close');
			},


			uploadImag() {
				uni.showActionSheet({
					itemList: ['查看头像', '从相册选择'],
					success: (res) => {
						let index = res.tapIndex;
						if (index == 0) {
							//查看头像
							var avatarArr = [];
							avatarArr.push(this.userinfo.photo.photoUrl)
							uni.previewImage({
								urls: avatarArr,
								current: avatarArr[0]
							})
						} else if (index == 1) {
							//选择头像上传
							uni.chooseImage({
								count: 1,
								sizeType: ["compressed"],
								sourceType: ["album"],
								success: (res) => {
									var filePath = res.tempFilePaths[0];
									//跳转相册选择组件
									let a = uni.uploadFile({
										url: `http://localhost:18001/api/user/upload/${this.userinfo.id}`,
										filePath: filePath,
										name: 'file',
										formData: {
											user: 'test'
										},
										success: (res) => {
											let data = res.data
											let dataObj = JSON.parse(data)
											console.log(dataObj)
											this.userinfo.photo = dataObj.data
											uni.setStorageSync("user", this.userinfo)

										}
									});
								}
							})
						}
					}
				})
			},
			
		},
		
		onShow() {
			this.checkLogin()
		},
		onLoad() {
			let token = uni.getStorageSync('token');
			this.token = token
			if (token == '') {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			} else {
				
			}
		}
	}
</script>
<style lang="scss">
	page {
		background-color: #fff
	}

	.header {
		&.status {
			padding-top: var(--status-bar-height);
		}

		background-color:#5DACD3;
		width:92%;
		height:30vw;
		padding:0 4%;
		display:flex;
		align-items:center;

		.userinfo {
			width: 90%;
			display: flex;

			.face {
				flex-shrink: 0;
				width: 15vw;
				height: 15vw;

				image {
					width: 100%;
					height: 100%;
					border-radius: 100%
				}
			}

			.info {
				display: flex;
				flex-flow: wrap;
				padding-left: 30upx;

				.username {
					width: 100%;
					color: #fff;
					font-size: 45upx
				}

				.integral {
					display: flex;
					align-items: center;
					padding: 0 20upx;
					height: 40upx;
					color: #fff;
					background-color: rgba(0, 0, 0, 0.1);
					border-radius: 20upx;
					font-size: 24upx
				}

			}
		}

	}
</style>
